<template>
  <div :class="['min-value-container', darkMode ? 'dark-mode' : 'light-mode']">
    <h3>MinValue组件</h3>
    
    <div class="input-section">
      <input 
        type="number" 
        v-model.number="inputValue" 
        placeholder="请输入数字" 
        :class="{ 'invalid': !isValidInput && inputValue !== '' }"
      />
      <button @click="addNumber" :disabled="!isValidInput">添加</button>
      <button @click="clearNumbers" class="clear-btn">清空</button>
      <button @click="toggleTheme" class="theme-btn">切换主题</button>
    </div>
    
    <div class="result-section">
      <p v-if="numbers.length > 0">最小值: <span class="min-value">{{ minValue }}</span></p>
      <p v-else>请添加数字以计算最小值</p>
      
      <div class="numbers-list" v-if="numbers.length > 0">
        <h4>已添加的数字:</h4>
        <ul>
          <li v-for="(num, index) in numbers" :key="index">{{ num }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MinValue',
  props: {
    modelValue: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      inputValue: '',
      numbers: this.modelValue || [],
      darkMode: false
    }
  },
  computed: {
    minValue() {
      if (this.numbers.length === 0) return null;
      return Math.min(...this.numbers);
    },
    isValidInput() {
      return this.inputValue !== '' && !isNaN(this.inputValue);
    }
  },
  methods: {
    addNumber() {
      if (this.isValidInput) {
        const num = Number(this.inputValue);
        this.numbers.push(num);
        this.inputValue = '';
        this.updateModel();
      }
    },
    clearNumbers() {
      this.numbers = [];
      this.updateModel();
    },
    updateModel() {
      this.$emit('update:modelValue', [...this.numbers]);
    },
    toggleTheme() {
      this.darkMode = !this.darkMode;
    },
    getValue() {
      return this.minValue;
    }
  }
}
</script>

<style scoped>
.min-value-container {
  margin: 20px 0;
  padding: 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.light-mode {
  background-color: #f8f9fa;
  border: 1px solid #dee2e6;
  color: #212529;
}

.dark-mode {
  background-color: #343a40;
  border: 1px solid #495057;
  color: #f8f9fa;
}

.input-section {
  display: flex;
  margin-bottom: 15px;
  flex-wrap: wrap;
  gap: 10px;
}

input {
  flex: 1;
  min-width: 120px;
  padding: 8px 12px;
  border: 1px solid #ced4da;
  border-radius: 4px;
  font-size: 16px;
}

.dark-mode input {
  background-color: #495057;
  color: #f8f9fa;
  border-color: #6c757d;
}

.invalid {
  border-color: #dc3545 !important;
}

button {
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s;
}

button:hover:not(:disabled) {
  background-color: #45a049;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.clear-btn {
  background-color: #dc3545;
}

.clear-btn:hover {
  background-color: #c82333;
}

.theme-btn {
  background-color: #6c757d;
}

.theme-btn:hover {
  background-color: #5a6268;
}

.result-section {
  margin-top: 20px;
}

.min-value {
  font-weight: bold;
  font-size: 18px;
  color: #4CAF50;
}

.dark-mode .min-value {
  color: #8bc34a;
}

.numbers-list {
  margin-top: 15px;
}

ul {
  list-style-type: none;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

li {
  padding: 5px 10px;
  background-color: #e9ecef;
  border-radius: 4px;
  color: #212529;
}

.dark-mode li {
  background-color: #495057;
  color: #f8f9fa;
}
</style>